<template>
    <div class="page-100 login-register-part">
        <yd-cell-item style="padding-bottom: 10px">
            <yd-input slot="right" type="password" v-model="password" placeholder="原密码"></yd-input>
        </yd-cell-item>
        <yd-cell-item style="padding-bottom: 10px">
            <yd-input slot="right" type="password" v-model="rawPasswd" placeholder="新密码"></yd-input>
        </yd-cell-item>
        <yd-button size="large" type="primary" @click.native="revisepasswordClick" style="background-color:#3cc8a8;color:white;height: 40px">确定</yd-button>
    </div>
</template>

<script>
    import {userInfoData,openId} from '../../globalVariables'
    import {revisePassword,loginout} from "../../pcof/post";
    export default {
        name: "revisePassword",
        data() {
            return {
                password: '',
                rawPasswd: '',

            };
        },
        methods:{
            async loginoutFunc()
            {
                var outresponse = await loginout(JSON.parse(localStorage.getItem(userInfoData)).mobile);
                if (outresponse.data.code === 200) {
                    this.$dialog.loading.close();
                    // localStorage.removeItem(openId);
                    localStorage.clear();
                    this.$router.push({name: 'loginPhone'});
                    this.$dialog.toast({
                        mes: '修改成功，请重新登录',
                        timeout: 1500
                    });
                } else {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: outresponse.data.msg,
                        timeout: 1500
                    });
                }
            },

            async revisepasswordClick ()
            {

                if (this.password === null || this.password === '') {
                    this.$dialog.toast({
                        mes: '请输入原密码!',
                        timeout: 1500
                    });
                    return;
                }
                if (this.rawPasswd === null || this.rawPasswd === '') {
                    this.$dialog.toast({
                        mes: '请设置新密码!',
                        timeout: 1500
                    });
                    return;
                }
                var exp=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/;

                if(!exp.test(this.rawPasswd))
                {
                    this.$dialog.toast({
                        mes: '新密码格式错误',
                        timeout: 1500
                    });
                    return;
                }
                var uuid=JSON.parse(localStorage.getItem(userInfoData)).uuid;
                var  response = await  revisePassword(JSON.parse(localStorage.getItem(userInfoData)).mobile,uuid,this.password,this.rawPasswd);
                this.$dialog.loading.open('修改中...');

                if (response.data.code === 200) {
                    this.$dialog.loading.close();

                    this.loginoutFunc();

                }else
                {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes:response.data.msg,
                        timeout: 1500
                    });
                }

            }

        }

    }
</script>
  <style lang='scss'>
    @import "../login/login.scss";
</style>